package utils

import config.MyFile
import org.apache.commons.io.FileUtils

import scala.jdk.CollectionConverters._

/**
 * BIG02服务器运行
 * 1.处理GFF文件，格式：chr geneid  start end ,dealGff
 * 2.blast建库: makeblastdb ,work1.sh
 * 3.blastp : work2.sh
 * 4.组合GFF : getGff
 * 5.运行 MCScanX
 *
 */


object MCScanX extends MyFile {


  def main(args: Array[String]): Unit = {
  //  dealGff
    //makeblastdb
    //blastp
    // getGff
  //   mscanx
  mergeBlockTable("H:\\南农油茶数据库\\mcscanx\\collinearity","H:\\南农油茶数据库\\mcscanx\\block.txt")

  }

  def mergeBlockTable(path: String, output: String) = {
    val r = path.listFile.flatMap { xx =>
      xx.readLines.filter(!_.startsWith("#")).map { x =>
        val line = x.split("\t")
        line(1) + "\t" + line(2)
      }
    }.toBuffer
    FileUtils.writeLines(output.toFile, r.asJava)
  }

  def mscanx = {
    val species = "H:\\南农油茶数据库\\mcscanx/pep".listFile.filter(_.getName.endsWith("pep")).map(_.getName.replace("pep","CON"))
    val work3 = species.zipWithIndex.flatMap { case (x, i) =>
      species.drop(i + 1).map { y =>
        val blast = x.dropRight(4) + "_vs_" + y.dropRight(4)
        s"/mnt/sdb/linait/tools/MCScanX/MCScanX  $blast  -m 50"
      }
    }
    "H:\\南农油茶数据库\\mcscanx\\mcscanX/work3.sh".writeLines(work3.toBuffer)
  }

  def dealGff = {
    "H:\\南农油茶数据库\\mcscanx/gff".listFile.filter(_.getName.endsWith("Camellia_crapnelliana.gff")).foreach { f =>
      var rows =  f.readLines.filter(!_.startsWith("#")).filter(_.trim != "").map(_.split("\t")).filter(_ (2) == "gene")
      if(rows.isEmpty){
       rows =   f.readLines.filter(!_.startsWith("#")).filter(_.trim != "").map(_.split("\t")).filter(_ (2) == "mRNA")
      }

      val row =rows.map { x =>
        println(f.getName,x.mkString("\t"))
        val geneid = x(8).split(";").find(_.startsWith("ID")).get.drop(3)

        Seq(x.head,geneid, x(3), x(4)).mkString("\t")
      }
      FileUtils.writeLines(s"H:\\南农油茶数据库\\mcscanx\\mcscanX/filter/${f.getName}".toFile, row.asJava)
    }
  }

  def makeblastdb = {
    val work1 = "H:\\南农油茶数据库\\mcscanx/pep".listFile.filter(_.getName.endsWith("pep")).map { x =>
      val name1 = x.getName
      val name = if(name1 == "pep.pep"){
        "CON.pep"
      }else{
        name1
      }
      s"makeblastdb -in $name -dbtype prot -parse_seqids -out $name"
    }
    "H:\\南农油茶数据库\\mcscanx\\mcscanX/work1.sh".writeLines(work1.toBuffer)
  }

  def blastp = {
    val species = "H:\\南农油茶数据库\\mcscanx/pep".listFile.filter(_.getName.endsWith("pep")).map(_.getName.replace("pep","CON"))
    val work2 = species.zipWithIndex.flatMap { case (x, i) =>
      species.drop(i + 1).map { y =>
        val blast = x.dropRight(4) + "_vs_" + y.dropRight(4) + ".blast"
        s"blastp -query $x -db $y -out $blast -evalue 1e-10 -num_threads 30 -outfmt 6 -num_alignments 10"
      }
    }
    "H:\\南农油茶数据库\\mcscanx\\mcscanX/work2.sh".writeLines(work2.toBuffer)
  }

  def getGff = {
    val species = "H:\\\\南农油茶数据库\\\\mcscanx\\\\mcscanX/filter".listFile.filter(_.getName.endsWith("gff"))
    species.zipWithIndex.foreach { case (x, i) =>
      species.drop(i + 1).foreach { y =>
        val gff = x.getName.dropRight(4).replace("pep","CON") + "_vs_" + y.getName.dropRight(4).replace("pep","CON")  + ".gff"
        s"H:\\南农油茶数据库\\mcscanx\\mcscanX\\filter2/$gff".writeLines(x.readLines ++ y.readLines)
      }
    }
  }

}
